/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/confirmOrder/tmp.html', 'payment', 'address', 'layer'], function (page, payment, address, layer) {

    var render = template.compile(page);

    var apiList = {
        getOrderInfo: api + 'order/check',
        postOrder: api + 'order'
    };

    var vm = new Page.creat({
        $id: 'confirmOrder',
        $model: {
            data: {},
            $: {},
            needPay: 0,
            init: function () {

                vm.$init({
                    setTitle: '确认订单'
                });

                var data = db.get('confirmOrder');

                if (data != null) {
                    vm.needPay = vm.action.computeNeedPay(data.amount, data.coupon);
                    vm.data = {
                        Pares_address: address.getAddress,
                        address: data.address || null,
                        skus: data.skus,
                        coupon: data.coupon || null,
                        amount: data.amount,
                        postage: data.postage,
                        discount: data.postage.cost + data.amount - vm.needPay,
                        allAmount: vm.needPay + (data.postage.lack ? data.postage.cost : 0)
                    };
                    $('#page').html(render(vm.data));

                    ex.async(function () {

                        vm.bindEvent(vm.event);
                        vm.bind$(vm.$);

                        if (vm.data.address != null) {
                            vm.$.fbutton.removeClass('un');
                        }

                    });
                } else {
                    //todo 滚去错误页，让你手贱点刷新
                    throw '滚去错误页，让你手贱点刷新'
                }

            },
            event: {
                clickfb: function () {
                    if ($(this).hasClass('un'))return;
                    console.log('REMARK:' + vm.$.remark.val());

                    var POSTDATA = {
                        addressId: vm.data.address._id,
                        skus: {}
                    };
                    $.map(vm.data.skus, function (v) {
                        POSTDATA.skus[v._id] = v.quantity
                    });
                    if (vm.data.coupon)POSTDATA.couponId = vm.data.coupon._id;
                    if (vm.$.remark)POSTDATA.remark = vm.$.remark.val();

                    ex.jsonp({
                        url: apiList.postOrder,
                        method: 'POST',
                        data: POSTDATA,
                        success: function (obj) {
                            console.log(obj);
                            if (obj.code == 0) {
                                obj = obj.data;
                                //Tips:此时的栈应当是 购物车->确认订单
                                payment.start({
                                    close: true,
                                    bill_id: obj.bill_id,
                                    order_id: obj.order_id,
                                    WXsuccess: function () {
                                        page_back_and_go('#/paySuccess/');
                                    },
                                    WXcancel: function () {
                                        page_back_and_go('#/error/redirect=/orderDetail/');
                                    }
                                });
                            } else {
                                layer.msg(obj.msg);
                            }
                        },
                        error: function () {
                            layer.msg('您的网络链接不太顺畅哦');
                        }
                    });

                    ////todo 结算
                    //throw '结算'
                }
            },
            action: {
                computeNeedPay: function (amount, coupon) {
                    //状态说明
                    // status: 0 可用 -1 不可用
                    // type:  0 折扣  1 满减
                    var discount;
                    if (coupon == null) {
                        return amount;
                    }
                    if (coupon.type == 0) {

                        discount = amount * (100 - coupon.zk) / 100;
                        console.log(coupon.minus);
                        discount = discount < coupon.minus ? discount : coupon.minus;
                    } else {
                        if (coupon.man <= amount) {
                            discount = coupon.minus;
                        } else {
                            discount = 0;
                        }
                    }
                    discount = Math.floor(discount);
                    return amount - discount;

                }
            }
        }
    });


    return {
        init: function () {
            vm.init();
        }
    }
});